package com.gz.purchase.app;

import android.app.Application;
import android.content.Intent;
import android.graphics.Bitmap;

import com.gz.purchase.R;
import com.gz.purchase.library.IAuthor;
import com.gz.purchase.library.IOrder;
import com.gz.purchase.library.IProduct;
import com.gz.purchase.library.util.CrashHandler;
import com.gz.purchase.library.util.PreferenceModel;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;


public class App extends Application {

	@Override
	public void onCreate() {
		super.onCreate();

		CrashHandler.init(this);
		IAuthor.init(this);
		IOrder.init(this);
		IProduct.init(this);
		PreferenceModel.init(this);

		// UnlimitedDiscCache 不限制缓存大小
		// TotalSizeLimitedDiscCache 对缓存大小限制。如果缓存大小超过规定限额然后文件最老的最后使用日期的将被删除
		// FileCountLimitedDiscCache 对文件数量限制，对最近最少使用的的删除
		// LimitedAgeDiscCache 限定一个日期，过期的将被删除
		
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.mipmap.ic_load_image_square_default)
        .showImageOnFail(R.mipmap.ic_load_image_square_fail)
        .showImageForEmptyUri(R.mipmap.ic_load_image_square_fail)
        .resetViewBeforeLoading(true)
        .bitmapConfig(Bitmap.Config.ARGB_8888)
        .cacheInMemory(true)
        .cacheOnDisk(true)
        .imageScaleType(ImageScaleType.EXACTLY) 
        .build();
		

		File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        	.threadPoolSize(3) // default
        	.threadPriority(Thread.NORM_PRIORITY - 1) // default
        	.tasksProcessingOrder(QueueProcessingType.LIFO) // default
        	.denyCacheImageMultipleSizesInMemory()
        	.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
        	.diskCache(new LimitedAgeDiscCache(cacheDir, 3600 * 24 * 7)) // 一个星期
        	.diskCacheFileNameGenerator(new Md5FileNameGenerator())       // default
        	.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
        	.imageDecoder(new BaseImageDecoder(true)) // default
        	.defaultDisplayImageOptions(options)    // default
        	.build();
		ImageLoader.getInstance().init(config);

		//启动清扫服务
		//startService(new Intent(getApplicationContext(), CleanService.class));
		

	}
	
}
